/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.net;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import com.feiyue.basic.Tool;
import com.feiyue.basic.server.cache.GZip;
import com.feiyue.basic.server.db.*;
import com.feiyue.basic.server.parser.*;
import com.info.runninginfo.ExceptionManager;
import com.info.runninginfo.RunningInfo;
import com.info.runninginfo.exception.BusinessException;

import java.io.IOException;
import java.util.List;

import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParserException;

public class NovelInfo
{

	public static List getBookList(Context context, String s, int i, int j, int k)
	{
		List list = null;
		
		try {
			if (j == 0) {
				BookDBHandler bookdbhandler1 = new BookDBHandler(context);
				list = bookdbhandler1.getBookList(context, i);
				bookdbhandler1.destroy();
				if (list != null) {
					int i1 = list.size();
					if (i1 != 0) {
						return list;
					}
				}
			}
			
			if (Tool.currentNet(context) != -1) {
				HttpGet httpget;
				httpget = new HttpGet(s);
				RunningInfo.outputRunningInfo(context, "获取小说信息时使用的url", s);
				DefaultHttpClient defaulthttpclient;
				BasicHttpParams basichttpparams = new BasicHttpParams();
				HttpConnectionParams.setConnectionTimeout(basichttpparams, 30000);
				HttpConnectionParams.setSoTimeout(basichttpparams, 30000);
				defaulthttpclient = new DefaultHttpClient();
				int l;
				
				HttpResponse httpresponse = defaulthttpclient.execute(httpget);
				l = httpresponse.getStatusLine().getStatusCode();
				String s1 = GZip.uncmprss(context, EntityUtils.toString(httpresponse.getEntity()));
				if (l != 200) {
					(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config失败，code=\r\n")).append(l).toString()));
				} else {
				    //TODO
					RunningInfo.outputRunningInfo(context, "返回值", s1);
					Xml2ParseBookList xml2parsebooklist = new Xml2ParseBookList(s1, i);
					BookDBHandler bookdbhandler = new BookDBHandler(context);
					bookdbhandler.insert(xml2parsebooklist.getBookList(), context);
					bookdbhandler.destroy();
					list = xml2parsebooklist.getBookList();
				}
			}
		} catch (Exception e) {
			(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
		}
		
		return list;
	}

	public static List getCategoryList(Context context, String s)
	{
		List list = null;
		int j;
		
		try {
			CategoryDBHandler categorydbhandler = new CategoryDBHandler(context);
			list = categorydbhandler.getCategoryList(context);
			categorydbhandler.destroy();
			j = list.size();
			if (j == 0) {
				if (Tool.currentNet(context) != -1) {
					HttpGet httpget;
					httpget = new HttpGet(s);
					RunningInfo.outputRunningInfo(context, "获取小说信息时使用的url", s);
					DefaultHttpClient defaulthttpclient;
					BasicHttpParams basichttpparams = new BasicHttpParams();
					HttpConnectionParams.setConnectionTimeout(basichttpparams, 30000);
					HttpConnectionParams.setSoTimeout(basichttpparams, 30000);
					defaulthttpclient = new DefaultHttpClient();
					int i;
					HttpResponse httpresponse = defaulthttpclient.execute(httpget);
					i = httpresponse.getStatusLine().getStatusCode();
					String s1 = GZip.uncmprss(context, EntityUtils.toString(httpresponse.getEntity()));
					if (i != 200) {
						(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config失败，code=\r\n")).append(i).toString()));
					} else {
					    //TODO
						RunningInfo.outputRunningInfo(context, "返回值", s1);
						Xml2ParseCategoryList xml2parsecategorylist = new Xml2ParseCategoryList(s1);
						CategoryDBHandler categorydbhandler1 = new CategoryDBHandler(context);
						categorydbhandler1.insert(xml2parsecategorylist.getCategoryList(), context);
						categorydbhandler1.destroy();
						list = xml2parsecategorylist.getCategoryList();
					}
				}
			}
		} catch (Exception e) {
			(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
		}
		
		return list;
	}

	public static List getChapterList(final Context context, String s, int i, int j, String s1, Handler handler)
	{
		List list = null;
		
		try {
			if (s1.equals("cache first")) {
				ChapterDBHandler chapterdbhandler1 = new ChapterDBHandler(context);
				list = chapterdbhandler1.getChapterList(context, i, j);
				chapterdbhandler1.destroy();
				if (list != null) {
					int l = list.size();
					if (l != 0) {
						return list;
					}
				}
			}
			if (Tool.currentNet(context) != -1) {
				HttpGet httpget;
				httpget = new HttpGet(s);
				RunningInfo.outputRunningInfo(context, "获取小说信息时使用的url", s);
				DefaultHttpClient defaulthttpclient;
				BasicHttpParams basichttpparams = new BasicHttpParams();
				HttpConnectionParams.setConnectionTimeout(basichttpparams, 30000);
				HttpConnectionParams.setSoTimeout(basichttpparams, 30000);
				defaulthttpclient = new DefaultHttpClient();
				int k;
				HttpResponse httpresponse = defaulthttpclient.execute(httpget);
				k = httpresponse.getStatusLine().getStatusCode();
				String s2 = GZip.uncmprss(context, EntityUtils.toString(httpresponse.getEntity()));
				if (k != 200) {
					(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config失败，code=\r\n")).append(k).toString()));
				} else {
				    //TODO
					RunningInfo.outputRunningInfo(context, "返回值", s2);
					Xml2ParseChapterList xml2parsechapterlist = new Xml2ParseChapterList(s2, i, j);
					if (xml2parsechapterlist.getChapterList() != null && xml2parsechapterlist.getChapterList().size() != 0)
					{
						ChapterDBHandler chapterdbhandler = new ChapterDBHandler(context);
						if (handler != null)
						{
							List list1 = chapterdbhandler.getChapterList(context, i, j);
							if (list1 != null && list1.size() != 0)
							{
								final int difference = xml2parsechapterlist.getChapterList().size() - list1.size();
								Runnable runnable = new Runnable() {
									public void run()
									{
										Toast.makeText(context, (new StringBuilder("更新了")).append(difference).append("章").toString(), 1).show();
									}
								};
								handler.post(runnable);
							}
						}
						chapterdbhandler.delete(j);
						chapterdbhandler.insert(xml2parsechapterlist.getChapterList(), context);
						chapterdbhandler.destroy();
					}
					list = xml2parsechapterlist.getChapterList();
				}
			}
		} catch (Exception e) {
			(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
		}
		
		return list;
	}

	public static Comment getComments(Context context, String s, int i, int j, int k)
	{
		Comment comment = null;
		if (Tool.currentNet(context) != -1) {
			HttpGet httpget;
			httpget = new HttpGet(s);
			RunningInfo.outputRunningInfo(context, "获取小说评论时使用的url", s);
			DefaultHttpClient defaulthttpclient;
			BasicHttpParams basichttpparams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(basichttpparams, 30000);
			HttpConnectionParams.setSoTimeout(basichttpparams, 30000);
			defaulthttpclient = new DefaultHttpClient();
			int l;
			
			try {
				HttpResponse httpresponse = defaulthttpclient.execute(httpget);
				l = httpresponse.getStatusLine().getStatusCode();
				String s1 = GZip.uncmprss(context, EntityUtils.toString(httpresponse.getEntity()));
				if (l != 200) {
					(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config失败，code=\r\n")).append(l).toString()));
				} else {
				    //TODO
//					RunningInfo.outputRunningInfo(context, "返回值", s1);
//					comment = (new Xml2ParseComment(s1, i)).getCommentList();
				}
			} catch (Exception e) {
				(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
			}
			
		}
		
		return comment;
	}

	public static List getNewestBookList(Context context, String s)
	{
		List list = null;
		if (Tool.currentNet(context) != -1) {
			HttpGet httpget;
			httpget = new HttpGet(s);
			RunningInfo.outputRunningInfo(context, "检索小说时使用的url", s);
			DefaultHttpClient defaulthttpclient;
			BasicHttpParams basichttpparams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(basichttpparams, 30000);
			HttpConnectionParams.setSoTimeout(basichttpparams, 30000);
			defaulthttpclient = new DefaultHttpClient();
			int i;
			
			try {
				HttpResponse httpresponse = defaulthttpclient.execute(httpget);
				i = httpresponse.getStatusLine().getStatusCode();
				String s1 = GZip.uncmprss(context, EntityUtils.toString(httpresponse.getEntity()));
				if (i != 200) {
					(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config失败，code=\r\n")).append(i).toString()));
				} else {
				    //TODO
					RunningInfo.outputRunningInfo(context, "返回值", s1);
					list = (new Xml2ParseBookList(s1, -1)).getBookList();
				}
			} catch (Exception e) {
				(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
			}
			
		}
		return list;
	}

	public static String getUrl2Booklist(int i, int j, int k)
	{
		Object aobj[] = new Object[3];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		aobj[2] = Integer.valueOf(k);
		return String.format("http://book.yy567.net/bookSummary?requestType=getClassBook&classid=%d&after=%d&length=%d", aobj);
	}

	public static String getUrl2ChapterContent(int i, int j)
	{
		Object aobj[] = new Object[2];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookContent?type=content&bookid=%d&contentid=%d", aobj);
	}

	public static String getUrl2Chapterlist(int i)
	{
		Object aobj[] = new Object[1];
		aobj[0] = Integer.valueOf(i);
		return String.format("http://book.yy567.net/bookContent?type=chapter&bookid=%d", aobj);
	}

	public static String getUrl2Comment(int i, int j, int k)
	{
		Object aobj[] = new Object[3];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		aobj[2] = Integer.valueOf(k);
		return String.format("http://book.yy567.net/bookComment?requestType=getComments&bookid=%d&after=%d&length=%d", aobj);
	}

	public static String getUrl2ReportComment(int i, String s, int j)
	{
		Object aobj[] = new Object[3];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = s;
		aobj[2] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookComment?requestType=reportComments&bookid=%d&comment=%s&score=%d", aobj);
	}

	public static String getUrl2boyhotBooklist(int i, int j)
	{
		Object aobj[] = new Object[2];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookSummary?requestType=boyHot&after=%d&length=%d", aobj);
	}

	public static String getUrl2girlhotBooklist(int i, int j)
	{
		Object aobj[] = new Object[2];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookSummary?requestType=girlHot&after=%d&length=%d", aobj);
	}

	public static String getUrl2hotBooklist(int i, int j)
	{
		Object aobj[] = new Object[2];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookSummary?requestType=getHot&after=%d&length=%d", aobj);
	}

	public static String getUrl2monthhotBooklist(int i, int j)
	{
		Object aobj[] = new Object[2];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookSummary?requestType=monthHot&after=%d&length=%d", aobj);
	}

	public static String getUrl2newestBooklist(int i, int j)
	{
		Object aobj[] = new Object[2];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookSummary?requestType=recommend&after=%d&length=%d", aobj);
	}

	public static String getUrl2searchBooklist(String s)
	{
		Object aobj[] = new Object[1];
		aobj[0] = s;
		return String.format("http://book.yy567.net/bookSummary?requestType=searchBook&searchWord=%s", aobj);
	}

	public static String getUrl2weekhotBooklist(int i, int j)
	{
		Object aobj[] = new Object[2];
		aobj[0] = Integer.valueOf(i);
		aobj[1] = Integer.valueOf(j);
		return String.format("http://book.yy567.net/bookSummary?requestType=weekHot&after=%d&length=%d", aobj);
	}

	public static List getsearchBookList(Context context, String s)
	{
		List list = null;
		if (Tool.currentNet(context) != -1) {
			HttpGet httpget;
			DefaultHttpClient defaulthttpclient;
			httpget = new HttpGet(s);
			RunningInfo.outputRunningInfo(context, "检索小说时使用的url", s);
			BasicHttpParams basichttpparams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(basichttpparams, 30000);
			HttpConnectionParams.setSoTimeout(basichttpparams, 30000);
			defaulthttpclient = new DefaultHttpClient();
			int i;
			
			HttpResponse httpresponse;
			try {
				httpresponse = defaulthttpclient.execute(httpget);
				i = httpresponse.getStatusLine().getStatusCode();
				String s1 = GZip.uncmprss(context, EntityUtils.toString(httpresponse.getEntity()));
				if (i != 200) {
					(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config失败，code=\r\n")).append(i).toString()));
				} else {
				    //TODO
//					RunningInfo.outputRunningInfo(context, "返回值", s1);
//					list = (new Xml2ParseBookList(s1, -1)).getBookList();
				}
			} catch (ClientProtocolException e) {
				(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
				e.printStackTrace();
			} catch (IOException e) {
				(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
				e.printStackTrace();
//			} catch (XmlPullParserException e) {
//				(new ExceptionManager()).saveException(context, NovelInfo.class, new BusinessException((new StringBuilder("获取config文件时发送异常\r\n")).append(e).toString()));
//				e.printStackTrace();
			} catch (Exception e) {
				(new ExceptionManager()).saveException(context, NovelInfo.class, e);
				e.printStackTrace();
			}
		}
		
		return list;
	}
}